package org.coderk.mybatis.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.coderk.mybatis.publisher.HelloEventPublisher;
import org.coderk.mybatis.publisher.HelloManEventPublisher;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.http.HttpStatus;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author: TanJingyu
 * @create: 2021-03-21 21:03
 **/
@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    private DataSource source;
    @Autowired
    private BeanFactory beanFactory;
    @Autowired
    private DispatcherServlet dispatcherServlet;
    @Autowired
    private List<ApplicationListener> applicationListeners;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private HelloEventPublisher helloEventPublisher;
    @Autowired
    private HelloManEventPublisher helloManEventPublisher;

    @GetMapping("/{domain}")
    public String tempt(@PathVariable String domain) {
        return "tempt...";
    }

    @GetMapping("/aaa")
    public String aaa() {
        return "aaa";
    }

    @GetMapping("/codert")
    public void codert(HttpServletResponse response) throws IOException {
        response.addHeader("WWW-Authenticate", "Basic realm=\"" + 1111 + "\"");
        response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
    }

    @GetMapping
    public String hello() throws SQLException, IOException {
//        Connection connection = source.getConnection();
//        helloEventPublisher.publish();
        helloManEventPublisher.publish();
        return "success";
    }

    @GetMapping("/self")
    public String self(HttpServletResponse response, HttpServletRequest request) throws IOException {

        PrintWriter writer = response.getWriter();
        writer.write("");
        writer.flush();

        StreamUtils.copy("abcd-", StandardCharsets.UTF_8, response.getOutputStream());
        return "test";
    }

    /**
     * 做这个测试的意义就是，最后响应体中的网页，也就是字符串，浏览器会做格式化渲染而已
     */
    @GetMapping("/index")
    public String index() {
        return "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "    <style>\n" +
                "        .demo{\n" +
                "            height: 100px;\n" +
                "            border: solid;\n" +
                "            width: 25%;\n" +
                "            display: inline-block;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "<div style=\"width: 100%\">\n" +
                "    <div style=\"width: 80%;color: red\">标题1</div>\n" +
                "    <div style=\"width: 20%\">\n" +
                "        <div>fdsfd</div>\n" +
                "        <div>fsdfsdfdfdfdf</div>\n" +
                "    </div>\n" +
                "</div>\n" +
                "</body>\n" +
                "</html>\n";
    }

}
